<?php
class ATools{
	/**
	 * 取配置
	 * @param string $name 配置名称
	 */
	function getConfig($name,$where=0){
		$path = $where ? JYAPI_PATH : APP_PATH;
		return JYTools::config($name,$path."config/");
	}
	
	// 从生日计算年龄
	public static function getAge($birth, $yearWord = '岁') {
		$birth = date('Y-m-d', strtotime($birth));
		$birth = explode('-', $birth);
		$y = $birth[0];
		$m = $birth[1];
		$d = $birth[2];
		
		$today = explode('-',date('Y-m-d'));
		if($today[0]<$y) return false;
		//足年的
		if($today[0]>$y){
			return $today[0]-$y.$yearWord;
		}
		//足月的
		if($today[1]>$m){
			return $today[1]-$m.'个月';
		}
		//足日的
		if($today[2]>$d){
			return $today[2]-$d.'天';
		}
		
		return false;
	}
	
	// 获得住院时年龄
	public static function getZhuyuanAge($zyAge) {
		$types = array('Y'=>'岁', 'M'=>'月', 'D'=>'天');
		if (!$zyAge) {
			return false;
		}
		$type = substr($zyAge, 0, 1);
		$data = substr($zyAge, 1);
		return $data.$types[$type];
	}
	
	// 获得回访状态
	public static function getVisitStatus($status) {
		if (!$status && $status !== '0') $status = 'A';
		if ((int) $status > 0 || $status === '0') {
			return $status.'次';
		}
		$ret = self::getConfig('app.visitStatus.'.$status);
		switch ($status) {
			case 'B':$ret = '<font color="#48bc41">'.$ret.'</font>';break;
			case 'C':;
			case 'H':;
			case 'I':;
			case 'G':;
			case 'A':$ret = '<font color="#bf0a01">'.$ret.'</font>';break;
			default:$ret = '<font color="#999999">'.$ret.'</font>';break;
		}
		return $ret;
	}
	
	// 数组处理
	public static function addSquote(&$arr) {
		if (is_array($arr)) {
			foreach ($arr as &$ar) {
				if (is_array($ar)) {
					self::addSquote($ar);
				} else {
					$ar = "'{$ar}'";
				}
			}
		}
	}
	
	public static function getFzDate($cyDate, $szPeriod) {
		$rs = '无需复诊';
		if ($szPeriod == '长期') {
			return $szPeriod;
		}
		$num = (int)$szPeriod;
		if ($num == 0) {
			return $rs;
		}
		$type = mb_substr($szPeriod, -1,1,'utf-8');
		switch ($type) {
			case '年':
				$time = date('Y-m-d', strtotime("+$num year", strtotime($cyDate)));
				break;
			case '月':
				$time = date('Y-m-d', strtotime("+$num month", strtotime($cyDate)));
				break;
			case '日':
				$time = date('Y-m-d', strtotime("+$num day", strtotime($cyDate)));
				break;
			case '周':
				$time = date('Y-m-d', strtotime("+$num week", strtotime($cyDate)));
				break;
			default:
				return '';
		}
		return $time;
	}
	
	public static function isAjax() {
		return strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest';
	}
	
	//取消对特殊字符的处理
	function unfilterText($str){
		//return $str;
		$str = str_ireplace("&amp;", "&", $str);

        $str = addslashes ( $str );
		$str = preg_replace (  "/&#036;/","\\$", $str );

		$str = str_ireplace ( "&#100;ocument.", "document.", $str );
		$str = str_ireplace ( "&#111;nerror","onerror",  $str );
		$str = str_ireplace (  "&#60;script","<script", $str );
		$str = str_ireplace ( "&#111;nsubmit","onsubmit",  $str );
		$str = str_ireplace ( "&#111;nload","onload",  $str );
		$str = str_ireplace ( "&#111;nclick", "onclick", $str );
		$str = str_ireplace ( "&#111;nmouseover", "onmouseover", $str );
		$str = str_ireplace ("&#097;bout:",  "about:", $str );
		$str = str_ireplace ( "&#097;lert", "alert", $str );

		$str = str_ireplace ( "j&#097;v&#097;script","javascript",  $str );

		$str = htmlspecialchars_decode ( $str, ENT_QUOTES );
		$str = str_ireplace("&nbsp;", "", $str);
		$str = str_ireplace(" ", "", $str);
		return $str;
	}
	
/**  
 +———————————————————-  
 * 字符串截取，支持中文和其他编码  
 +———————————————————-  
 * @static  
 * @access public  
 +———————————————————-  
 * @param string $str 需要转换的字符串  
 * @param string $start 开始位置  
 * @param string $length 截取长度  
 * @param string $charset 编码格式  
 * @param string $suffix 截断显示字符  
 +———————————————————-  
 * @return string  
 * @author huangzy@51094.com
 +———————————————————-  
 */ 
	function msubstr($str, $start=0, $length, $charset='utf-8', $suffix=true){  
	    if(function_exists('mb_substr'))  
	        return mb_substr($str, $start, $length, $charset);  
	    elseif(function_exists('iconv_substr')) {  
	        return iconv_substr($str,$start,$length,$charset);  
	    }  
	    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";  
	    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";  
	    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";  
	    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";  
	    preg_match_all($re[$charset], $str, $match);  
	    $slice = join('',array_slice($match[0], $start, $length));  
	    if($suffix) return $slice."……";  
	    return $slice;  
	} 

/**
 +———————————————————-
 * 秒转换成年-月-天-时-分-秒格式
 +———————————————————- 
 *@param string $str 需要转换的秒值
 +———————————————————-  
 * @return 年-月-天-时-分-秒 格式的string  
 * @author huangzy@51094.com
 +———————————————————- 
 */
	function formatSecond($str){
		if($str<0)return 0;
		$years = floor($str/31536000);
		$rem = $str%31536000;
		$months = floor($rem/(3600*24*30));
		$rem = $rem%(3600*24*30);
		$days = floor($rem/(3600*24));
		$rem = $rem%(24*3600);
		$hours = floor($rem/3600);
		$rem = $rem%3600;
		$minutes = floor($rem/60);
		$seconds = $rem%60;
		$ret .= $years?$years.'<font color="#999999">年</font>&nbsp;':'';
		$ret .= $months?$months.'<font color="#999999">个月</font>&nbsp;':'';
		$ret .= $days?$days.'<font color="#999999">天</font>&nbsp;':'';
		$ret .= $hours?$hours.'<font color="#999999">小时</font>&nbsp;':'';
		$ret .= $minutes?$minutes.'<font color="#999999">分钟</font>&nbsp;':'';
		$ret .= $seconds?$seconds.'<font color="#999999">秒</font>':'';
		
		return $ret;
	}

/*
 * 根据回访用户名返回真实姓名
 * 
 * 真实名存在：返回
 * 不存在：返回用户名
 */
	function retUserTrueName($username){
		$ret = JYDB::find('SYSUSER','user_name=\''.$username.'\'','TRUE_NAME TN');
		if(!empty($ret['TN'])){
			return $ret['TN'];
		}
		return $username;
	}
}